﻿package common 
{
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.display.Bitmap;
	import flash.geom.Matrix;
	
	/**
	 * ゲームの各シーンを表します。
	 * 
	 * ゲームのメインシーンを呼び出すには、
	 * main.invokeMainScene を使います。
	 */
	public class Scene 
	{
		[Embed(source = '../../lib/arrow.png')]
		private static var BitmapArrow:Class;
		

		public function Scene(main:MainSprite):void {
			this._main = main;
		}
		
		private var _main:MainSprite;
		protected function get main():MainSprite {
			return _main;
		}
		
		protected var _sprite:Sprite;
		
		public function onKeyDown(keyCode:KeyCode):void {
		}
		public function onKeyUp(keyCode:KeyCode):void {
		}
		
		protected function invokeMapScene(isFirst:Boolean, isGoal:Boolean):void {
			main.invokeMapScene(isFirst, isGoal);
		}
		protected function invokeMainScene(origin:uint, destination:uint):void {
			main.invokeMainScene(origin, destination);
		}
		private var arrow:Array = new Array();
		protected function setVisibleArrow(b:Boolean):void {
			if (b) {
				_sprite.addChild(arrow[0] = makeArrow(KeyCode.UP, 0));
				_sprite.addChild(arrow[1] = makeArrow(KeyCode.RIGHT, 90));
				_sprite.addChild(arrow[2] = makeArrow(KeyCode.DOWN, 90 * 2));
				_sprite.addChild(arrow[3] = makeArrow(KeyCode.LEFT, 90 * 3));
//				_sprite.addChild(arrow[4] = makeOK(KeyCode.OK));
			} else {
				for (var i:int; i < arrow.length; i++)
					_sprite.removeChild(arrow[i]);
			}
		}
		private const ARROW_X:int = 550;
		private const ARROW_Y:int = 400;
		private function makeArrow(key:KeyCode, deg:Number):Sprite {
			var sp:Sprite = new Sprite();
			var bmp:Bitmap = new BitmapArrow();
			sp.addChild(bmp);
			sp.buttonMode = true;
			var m:Matrix = new Matrix();
			m.translate(-bmp.width / 2, -bmp.height * 1.5);
			m.scale(.5, .5);
			m.rotate(deg / 180 * Math.PI);
			m.translate(ARROW_X, ARROW_Y);
			sp.transform.matrix = m;
//			sp.rotation = deg; // / 180 * Math.PI;
			sp.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
				onKeyDown(key);
			});
			return sp;
		}
		private function makeOK(key:KeyCode):Sprite {
			var sp:Sprite = new Sprite();
			sp.graphics.beginFill(0xFFFFFF);
			sp.graphics.drawCircle(0, 0, 20);
			sp.buttonMode = true;
			var m:Matrix = new Matrix();
			m.translate(ARROW_X, ARROW_Y);
			sp.transform.matrix = m;
			sp.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
				onKeyDown(key);
			});
			return sp;
		}
		
	}
	
}